package com.ckjava.test.web;

import com.ckjava.test.component.HandlerContext;
import com.ckjava.test.entity.UserEntity;
import com.ckjava.test.handler.AbstractHandler;
import com.ckjava.test.service.DBService;
import com.ckjava.test.service.UserService;
import com.ckjava.xutils.Constants;
import com.ckjava.xutils.http.HttpResponse;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api
@RestController
@RequestMapping(value="/user", produces = "application/json;charset=utf-8")
public class UserController implements Constants {

	@Autowired
	private DBService dbService;
	@Autowired
	private UserService userService;

	@Autowired
	private HandlerContext HandlerContext;

	@GetMapping(value="/sayHello")
	public HttpResponse<String> sayHello(
			@RequestParam String name) {
		return HttpResponse.getReturn("你好 ".concat(name), HTTPCODE.code_200, null);
	}

	@GetMapping(value="/getUser")
	public HttpResponse<UserEntity> getUser(
			@RequestParam String name) {
		UserEntity userEntity = userService.getUser(name);

		AbstractHandler handler = HandlerContext.getInstance(userEntity.getAge());
		handler.handle(userEntity);

		return HttpResponse.getReturn(userService.getUser(name), HTTPCODE.code_200, null);
	}

	@GetMapping(value="/getUserAsync")
	public HttpResponse<UserEntity> getUserAsync(
			@RequestParam String name) throws Exception {
		return HttpResponse.getReturn(userService.getUserAsync(name), HTTPCODE.code_200, null);
	}

	@GetMapping(value="/getDbInfo")
	public HttpResponse<String> dbService() {
		return HttpResponse.getReturn(dbService.toString(), HTTPCODE.code_200, null);
	}
}
